package com.liao.examine;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.liao.app2.R;
import com.liao.app2.ui.bean.User;

public class ItemListviewLayoutAdapter extends BaseAdapter {

    private List<User> users = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemListviewLayoutAdapter(Context context,ArrayList<User>users) {
        this.context = context;
        this.users = users;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public User getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_listview_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(User user, ViewHolder holder) {
        holder.tvName.setText(user.getName());
        holder.tvPwd.setText(user.getPw());
        holder.tvSex.setText(user.getSex());
    }

    protected class ViewHolder {
        private TextView tvName;
        private TextView tvPwd;
        private TextView tvSex;

        public ViewHolder(View view) {
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvPwd = (TextView) view.findViewById(R.id.tv_pwd);
            tvSex = (TextView) view.findViewById(R.id.tv_sex);
        }
    }
}
